package com.zhang.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.common.util.result.R;
import com.zhang.common.util.result.RUtils;
import com.zhang.system.dto.HomeAdvSearchDTO;
import com.zhang.system.entity.HomeAdv;
import com.zhang.system.service.IHomeAdvService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 首页轮播广告 前端控制器
 * </p>
 *
 * @author 创世神
 * @since 2024-06-05
 */
@RestController
@RequestMapping("/system/homeAdv")
public class HomeAdvController {
    @Autowired
    private IHomeAdvService homeAdvService;

    @PostMapping("apply")
    public R applyHomeAdv(@RequestBody @Valid HomeAdv homeAdv) {
        homeAdv.setPublisherId(StpUtil.getLoginIdAsLong());
        homeAdvService.save(homeAdv);
        return RUtils.successMsg("申请成功！等待审核");
    }

    @GetMapping("id")
    public R getHomeAdvById(@RequestParam String homeAdvId) {
        HomeAdv byId = homeAdvService.getById(homeAdvId);
        return RUtils.successData(byId);
    }

    @GetMapping("list")
    public R getHomeAdvList(HomeAdvSearchDTO homeAdvSearchDTO) {
        Page<HomeAdv> page = homeAdvService.getHomeAdvList(homeAdvSearchDTO);
        return RUtils.successData(page);
    }

    @PutMapping("inspect")
    @SaCheckPermission("system-homeAdv-inspect")
    public R inspectHomeAdv(@RequestBody HomeAdv homeAdv) {
        homeAdv.setAuthId(StpUtil.getLoginIdAsLong());
        homeAdvService.updateById(homeAdv);
        return RUtils.successMsg("修改成功");
    }

    /**获取系统正在使用的首页广告
     * @return {@link R}
     */
    @GetMapping("usingHomeAdv")
    public R getUsingHomeAdv() {
      List<HomeAdv> list= homeAdvService.getUsingHomeAdvList();
      return RUtils.successData(list);
    }
}
